package com.fengmi.service;

import cn.hutool.json.JSONUtil;
import com.api.cms.CmsApi;
import com.api.goods.GoodsApi;
import com.fengmi.cms.entity.Content;
import com.fengmi.goods.vo.GoodsCatVo;
import com.fengmi.goods.vo.HotGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import cn.hutool.*;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CacheService implements ICacheService{

   @Autowired
   private StringRedisTemplate redisTemplate;

   @Autowired
   private CmsApi cmsApi;


   @Autowired
   private GoodsApi goodsApi;

    /**
     * hash
     * key=>menhu:index:cms
     * field=>${catId}
     * value=> List<cotnent>(json)
     * @param catId
     * @return
     */
    @Override
    public List<Content> cacheContent(String catId) {

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps("menhu:index:cms");

        Object contentListJson = operations.get(catId);


        if (StringUtils.isEmpty(contentListJson)) {
            //从数据库获取（远程服务调用）
            List<Content> contentListByCatId = cmsApi.findContentListByCatId(catId);

            //缓存
            operations.put(catId, JSONUtil.toJsonStr(contentListByCatId));

            return  contentListByCatId;

        }


        return  JSONUtil.toList((String) contentListJson,Content.class);

    }


    /**
     *String
     * key:menhu:index:cat
     * value:  List<GoodsCatVo>(json)
     * @return
     */
    @Override
    public List<GoodsCatVo> cacheGoodsCat() {

        BoundValueOperations<String, String> operations = redisTemplate.boundValueOps("menhu:index:cat");
        String goodsCatListJson = operations.get();
        if(StringUtils.isEmpty(goodsCatListJson)){ //redis没命中

            //远程调用goods服务
            List<GoodsCatVo> goodsCatVos = goodsApi.cascade();

            //缓存
            operations.set(JSONUtil.toJsonStr(goodsCatVos));


            return goodsCatVos;

        }


        return JSONUtil.toList(goodsCatListJson,GoodsCatVo.class);
    }

    /**
     * key:menhu:index:hot
     *field:${catId}
     * value: List<HotGoodsVo>=>json
     * @return
     */

    @Override
    public List<HotGoodsVo> cacheHotGoods() {

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps("menhu:index:hot");
        List<Object> operationsListJson = operations.values();

        if (operationsListJson == null || operationsListJson.size() == 0 ) {
            //远程调用
            List<HotGoodsVo> hotGoods = goodsApi.findHotGoods();
            hotGoods.forEach(hotGoodsVo -> {
                operations.put(hotGoodsVo.getCat().getId()+"",JSONUtil.toJsonStr(hotGoodsVo));
            });


            return  hotGoods;
        }

        List<HotGoodsVo> collect = operationsListJson.stream().map(jsonstr -> JSONUtil.toBean((String) jsonstr, HotGoodsVo.class)).collect(Collectors.toList());

        return collect;
    }
}
